var devCenterData = []
//初始化界面
function init() {
    getData ()
    initVue()
}
//获取目录数据
function getData () {
    let results = sendAjax(modelURL + "resource/getDevelopmentCenterMenu", "POST");
    if (results.code == 200) {
        devCenterData = results.data;
        return devCenterData;
    }
}
//设置方法监听
function initVue() {
    new Vue({
        el: "#Main",
        data() {
            return {
                devCenterData: devCenterData,
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                ruleForm: {
                    directoryNameF:'',
                    directoryName: '',
                    interfaceName: '',
                    interfaceType: '',
                    introducedFun: '',
                    servicAddress: '',
                    requestParmse: '',
                    returnParmse: '',
                    codeExamples: '',
                    resource: '目录',
                },
                pid:'',
                rules: {
                    directoryName: [{
                        required: true,
                        message: '请输入目录名称',
                        trigger: 'blur'
                    }],
                    interfaceName: [{
                        required: true,
                        message: '请输入接口名称',
                        trigger: 'blur'
                    }],
                    interfaceType: [{
                        required: true,
                        message: '请输入接口类型',
                        trigger: 'blur'
                    }],
                    requesttype: [{
                        required: true,
                        message: '请输入请求类型',
                        trigger: 'blur'
                    }],
                    introducedFun: [{
                        required: true,
                        message: '请输入功能介绍',
                        trigger: 'blur'
                    }],
                    servicAddress: [{
                        required: true,
                        message: '请输入服务地址',
                        trigger: 'blur'
                    }],
                    requestParmse: [{
                        required: true,
                        message: '请输入请求参数',
                        trigger: 'blur'
                    }],
                    returnParmse: [{
                        required: true,
                        message: '请输入返回参数',
                        trigger: 'blur'
                    }],
                    codeExamples: [{
                        required: true,
                        message: '请输入代码实例',
                        trigger: 'blur'
                    }],
                    directoryNameF: [{
                        required: true,
                        message: '请输入根目录',
                        trigger: 'blur'
                    }],

                },
            };
        },
        methods: {
            handleNodeClick(data) {
                this.ruleForm.directoryNameF = data.label
                this.pid = data.id 
            },
            submitForm(formName) { 
                let obj = {},requestData = []
                let attrTableParam = attrTableData;
                for(var i = 0;i<attrTableParam.length;i++){
                    delete attrTableParam[i].LAY_TABLE_INDEX;
                    var index = 0,len =0;
                    for(var j in attrTableParam[i]){
                        len++
                        if(attrTableParam[i][j] == ""){
                            index++
                        }
                    }
                    if(index != len){
                        requestData.push(attrTableParam[i]);
                    }
                }
                requestData = JSON.stringify(requestData)
                if (this.newResource == '0') {
                    obj = {
                        name:this.ruleForm.interfaceName,
                        type:this.ruleForm.interfaceType,
                        description:this.ruleForm.introducedFun,
                        call:this.ruleForm.servicAddress,
                        inputvalue:requestData,
                        returnvalue:this.ruleForm.returnParmse,
                        codeexample:this.ruleForm.codeExamples,
                        ismenu:this.newResource,
                        pid:this.pid,
                        requesttype: this.ruleForm.requesttype,
                        sort: this.ruleForm.sort
                    }
                } else if (this.newResource == '1') { // 菜单
                    obj = {
                        name:this.ruleForm.directoryName,
                        ismenu:this.newResource,
                        pid:this.pid
                    }
                } 
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let results = sendAjax(modelURL + "resource/saveDevelopmentCenter", "POST",obj);
                        if (results.code == 200) {
                            this.$message({
                                message: results.msg, 
                                type: 'success'
                            });
                            var resultsData = getData ();
                            initVue()
                            return resultsData
                        }else{
                            this.$message.error("添加失败！")
                            return false;
                        }
                    } else {
                        

                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        computed: {
            newResource() {
                let resourceNum = null
                if (this.ruleForm.resource == '目录') {
                    resourceNum = '1'
                } else if (this.ruleForm.resource == '接口') {
                    resourceNum = '0'
                }
                return resourceNum
            },
        },
        watch:{
            newResource : {
                handler:function(){
                    if (this.newResource == '0') {
                        this.rules.interfaceName[0].required = true 
                        this.rules.directoryName[0].required = false
                        // this.rules.interfaceType[0].required = true
                        // this.rules.introducedFun[0].required = true
                        // this.rules.servicAddress[0].required = true
                        // this.rules.requestParmse[0].required = true
                        // this.rules.returnParmse[0].required = true
                        // this.rules.codeExamples[0].required = true
                    } else if (this.newResource == '1') { // 菜单
                        this.rules.interfaceName[0].required = false
                        this.rules.directoryName[0].required = true 
                        this.rules.interfaceType[0].required = false 
                        this.rules.introducedFun[0].required = false 
                        this.rules.servicAddress[0].required = false 
                        this.rules.requestParmse[0].required = false 
                        this.rules.returnParmse[0].required = false 
                        this.rules.codeExamples[0].required = false 
                    } 
                },
                immediate: true
            }
        }
    })
}
//初始化表格
function initTable() {
    var requestData = [];
    for(var i = 0;i<10;i++){
        requestData.push({
            "name":"",
            "meaning":"",
            "type":"",
            "require":"",
            "comments":"",
        })
    }
    var cols = [[
        { field: 'name', title: '参数名称', align: 'center',edit: 'text' }
        , { field: 'meaning', title: '参数含义', align: 'center',edit: 'text' }
        , { field: 'type', title: '字段类型', align: 'center',edit: 'text' }
        , { field: 'require', title: '是否必须', align: 'center',edit: 'text' }
        , { field: 'comments', title: '备注', align: 'center',edit: 'text' }
    ]];
    table.render({
        elem: '#requestParamsTable'
        , data: requestData
        , toolbar: false
        , cols: cols
        , page: false
        , response: {
            statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
        }
        , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.status, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.rows.item //解析数据列表
            };
        }
        ,done: function(res, curr, count) {
            attrTableData = layui.table.cache['requestParamsTable'];
        }
    });
}
//发送ajax请求接口
function sendAjax(url, type, data) {
    $.ajaxSettings.async = false;
    var results;
    $.ajax({
        type: type,
        dataType: "json",
        url: url,
        data: data,
        success: function (data) {
            results = data;
        },
        error: function (error) {
            results = error;
        }
    });
    return results;
}